package com.hl.card.pay.service.impl;

import com.hl.card.common.persistence.model.DistinguishIdCard;
import com.hl.card.common.persistence.model.User;
import com.hl.card.common.util.ResponseModel;
import com.hl.card.common.util.ResponseObject;
import com.hl.card.modular.pack.service.UserDataService;
import com.hl.card.modular.system.service.ISysDicService;
import com.hl.card.pay.dto.CollectDto;
import com.hl.card.pay.service.CommonService;
import com.hl.card.pay.vo.ChangeVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class CommonServiceImpl implements CommonService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Value("${pay.redis.payPassword.errorPrefix}")
    private String errorPrefix;

    @Value("${pay.redis.exchange.daylimitPrefix}")
    private String daylimitPrefix;//每日限额到达量

    @Value("${pay.redis.exchange.monthlimitPrefix}")
    private String monthlimitPrefix;//每月限额到达量

    @Autowired
    private ISysDicService sysDicService;

    @Autowired
    private UserDataService userDataService;

    @Override
    public Integer findPayError(String userId) {
        String   rechageStr=  redisTemplate.opsForValue().get(errorPrefix+userId);
        if(StringUtils.isBlank(rechageStr)){
            return 0;
        }else{
            return Integer.parseInt(rechageStr);
        }
    }
    /**
     *
     * @param userId
     * @param payPassword
     * @return
     */
    @Override
    public boolean checkPayPassWord(String userId, String payPassword) {
        String key=errorPrefix+userId;
        Boolean b = userDataService.checkPayPassword(userId, payPassword);
        if(!b){
            String   rechageStr=  redisTemplate.opsForValue().get(key);
            if(StringUtils.isBlank(rechageStr)){
                redisTemplate.opsForValue().set(key,"1",1l, TimeUnit.DAYS);
            }else{
                int value=Integer.parseInt(rechageStr);
                if(value>4){//超过了限制次数
                }else{
                    redisTemplate.opsForValue().set(key,value+1+"",1l, TimeUnit.DAYS);
                }
                redisTemplate.opsForValue().set(key,"1",1l, TimeUnit.DAYS);
            }
            return false;
        }
        return true;
    }

    @Override
    public ResponseModel checkChangeDto(ChangeVo changeVo) {
        ResponseModel responseModel=new ResponseModel();
        String amount=changeVo.getAmount();
        int amountInt=Integer.parseInt(amount);
        //每月限额已用额度
        String  monthArrive=redisTemplate.opsForValue().get(changeVo.getUserId() + monthlimitPrefix);
        Integer monthArriveInt=0;
        //每日额度到达量
        String  dayArrive=redisTemplate.opsForValue().get(changeVo.getUserId() + daylimitPrefix);
        Integer dayArriveInt=0;
        if(dayArrive!=null){
            dayArriveInt=Integer.parseInt(dayArrive);
        }
        if(monthArrive!=null){
            monthArriveInt=Integer.parseInt(monthArrive);
        }

        String day_limit = getDicValue("BDDHPZ", "PER_DAY_MAX");
        String mouth_limit = getDicValue("BDDHPZ", "PER_MOUTH_MAX");

        int day_amount_limit = Integer.parseInt(day_limit);
        int mouth_amount_limit = Integer.parseInt(mouth_limit);

        int monthRest=mouth_amount_limit-monthArriveInt-amountInt;
        int dayRest=day_amount_limit-dayArriveInt-amountInt;
        if(amountInt>day_amount_limit){
            responseModel.setStatus("T0003");
            responseModel.setData("超出单日限额");
            return responseModel;
        }
        if(monthRest<0){
            responseModel.setStatus("T0004");
            responseModel.setData("超出每月限额");
            return responseModel;
        }
        if(dayRest<0){
            responseModel.setStatus("T0003");
            responseModel.setData("超出单日限额");
            return responseModel;
        }
        responseModel.setStatus("T0000");
        responseModel.setStatus("检测通过");
        return responseModel;
    }


    @Override
    public Boolean setChangeDatLimit(ChangeVo changeVo) {
        String amount=changeVo.getAmount();
        int amountInt=Integer.parseInt(amount);
        String  monthArrive=redisTemplate.opsForValue().get(monthlimitPrefix);
        Integer monthArriveInt=0;
        if(monthArrive!=null){
            monthArriveInt=Integer.parseInt(monthArrive);
        }
        String  dayArrive=redisTemplate.opsForValue().get(daylimitPrefix);
        Integer dayArriveInt=0;
        if(dayArrive!=null){
            dayArriveInt=Integer.parseInt(dayArrive);
        }
        int monthAll=amountInt+monthArriveInt;
        int dayAll=amountInt+dayArriveInt;
        redisTemplate.opsForValue().set(daylimitPrefix,dayAll+"",1,TimeUnit.DAYS);
        redisTemplate.opsForValue().set(monthlimitPrefix,monthAll+"",30,TimeUnit.DAYS);
        return null;
    }

    private String getDicValue(String Code, String No) {
        ResponseObject res = new ResponseObject();
        res.setErroCode(1);
        res.setData("信息不存在");
        Map map=new HashMap<>();
        String value = null;
//        String version = stringRedisTemplate.opsForValue().get("VERSION" + ":" + plantform);
//        if(StringUtils.isNotBlank(version)) {
//            res.setErroCode(0);
//            map.put("version", version);
//            map.put("msg", "版本获取成功");
//            res.setData(map);
//        }else {
        Map<String, Object> item = sysDicService.selectListByCodeNo(Code, No);
        if(item != null) {
            value = (String) item.get("dicValue");
        }
        return value;
    }
}
